#!/usr/bin/env python

import random
import string
import MySQLdb as mysql




def genMaintenance():
    # maintenance fields
    warranty_length = [1,2,5,10,-1]
    return repr(str(random.choice(warranty_length)))

def genSafety():
    # safety fields
    reverse_camera = alarm = onstar = fuel_cutoff = [0,1]
    airbags = range(2,13,2)
    
    temp = "'" + str(random.choice(airbags)) + "',"
    temp += "'" + str(random.choice(reverse_camera)) + "',"
    temp += "'" + str(random.choice(alarm)) + "',"
    temp += "'" + str(random.choice(onstar)) + "',"
    temp += "'" + str(random.choice(fuel_cutoff)) + "'"
    return temp
    

def genExtras():
    # extras fields
    sport = cd = dvd = keyless_entry = [0,1]
    cruise_control = powered_seats = heated_seats = computer = [0,1]
    speakers = range(2,13,2)

    temp = "'" + str(random.choice(sport)) + "',"
    temp += "'" + str(random.choice(cd)) + "',"
    temp += "'" + str(random.choice(dvd)) + "',"
    temp += "'" + str(random.choice(speakers)) + "',"
    temp += "'" + str(random.choice(keyless_entry)) + "',"
    temp += "'" + str(random.choice(cruise_control)) + "',"
    temp += "'" + str(random.choice(powered_seats)) + "',"
    temp += "'" + str(random.choice(heated_seats)) + "',"
    temp += "'" + str(random.choice(computer)) + "'"
    return temp

def genHandling():
    # handling field
    power_steering = anti_lock = traction_control = all_wheel = [0,1]

    temp = "'" + str(random.choice(power_steering)) + "',"
    temp += "'" + str(random.choice(anti_lock))  + "',"
    temp += "'" + str(random.choice(traction_control))  + "',"
    temp += "'" + str(random.choice(all_wheel))  + "'"
    return temp
    
def genModel():
    # model fields
    models = ["Corolla","Camry","FJ","Supra","Land Cruiser","Yaris","RAV4","Prado"]
    years = range(1996,2014)
    cylinders = [4,6,8]
    horse_power = range(200,501,50)
    torque = range(100,160,5)
    automatic = [0,1]
    inline = [0,1,2]

    temp = "'" + random.choice(models) + "',"
    temp += "'" + str(random.choice(years)) + "',"
    temp += "'" + str(random.choice(cylinders)) + "',"
    temp += "'" + str(random.choice(horse_power)) + "',"
    temp += "'" + str(random.choice(torque)) + "',"
    temp += "'" + str(random.choice(automatic)) + "',"
    temp += "'" + str(random.choice(inline)) + "'"
    return temp
    
def genDesign():
    # design fields
    wheels = ['stock','premium','chrome','spinners','racing']
    leather = [0,1]
    interior_color = ['brown','black','biege']
    exterior_color = ['white','black','silver','red','yellow','blue','pink']

    temp = "'" + random.choice(wheels) + "',"
    temp += "'" + str(random.choice(leather)) + "',"
    temp += "'" + random.choice(interior_color) + "',"
    temp += "'" + random.choice(exterior_color) + "'"
    return temp
    
if __name__ == "__main__":
    # print "Model, Year, Cylinders, Horses, Torque, Automatic, Inline, Wheels, Leather, Interior, Exterior"
    try:
        db = mysql.connect("127.0.0.1","root","","toyota")
        db.autocommit(True)
        cur = db.cursor()
        
        file = open("vins.txt","r")
        vins = []
        for item in range(100):
            vins.append(file.readline().rstrip())
        for item in vins:
            cur.execute("INSERT INTO model(vin,name,year,cylinders,horse_power,torque,automatic,inline) VALUES(" + repr(item) + "," + genModel() + ");")
            # cur.execute("INSERT INTO design(vin,wheels,leather,interior_color,exterior_color) VALUES(" + repr(item) + "," + genDesign() + ");")
            # cur.execute("INSERT INTO handling(vin,power_steering,anti_lock,traction_control,all_wheel) VALUES(" + repr(item) + "," + genHandling() + ");")
            # cur.execute("INSERT INTO extras(vin,sport,cd,dvd,speakers,keyless_entry,cruise_control,powered_seats,heated_seats,computer) VALUES(" + repr(item) + "," + genExtras() + ");")
            # cur.execute("INSERT INTO extras(vin,sport,cd,dvd,speakers,keyless_entry,cruise_control,powered_seats,heated_seats,computer) VALUES(" + repr(item) + "," + genExtras() + ");")
            # cur.execute("INSERT INTO safety(vin,airbags,reverse_camera,alarm,onstar,fuel_cutoff) VALUES(" + repr(item) + "," + genSafety() + ");")
        data = cur.fetchone()
        print data
    except mysql.Error, e:
        print "Error: {}".format(e.args[1])
    finally:
        if db:
            db.close()